package com.content.scaffold.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.content.scaffold.common.annotation.EncryptParam;
import com.content.scaffold.common.event.BaseAsyncEvent;
import com.content.scaffold.common.event.constant.EventConstant;
import com.content.scaffold.common.exception.BaseException;
import com.content.scaffold.common.mybatis.SuperServiceImpl;
import com.content.scaffold.common.mybatis.query.QueryFieldWrapperUtil;
import com.content.scaffold.common.web.ResponseCodeEnum;
import com.content.scaffold.mapper.SysUserMapper;
import com.content.scaffold.model.dto.login.LoginDto;
import com.content.scaffold.model.dto.sys.QueryUserPage;
import com.content.scaffold.model.dto.sys.UserQuery;
import com.content.scaffold.model.entity.SysUser;
import com.content.scaffold.service.sys.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaojl
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    final private SysUserMapper sysUserMapper;
    final private ApplicationEventPublisher publisher;
    @Override
    public SysUser queryLoginUser(LoginDto loginDto) {
        List<SysUser> list = lambdaQuery().eq(SysUser::getUserCode, loginDto.getUserCode()).list();
        if (ObjectUtil.isEmpty(list)){
          throw  new BaseException(ResponseCodeEnum.ERROR,"账号或密码错误，请确认");
        }
        return list.stream().findFirst().orElse(null);
    }

    @Override
    @EncryptParam
    public Page<SysUser> queryUserPage(QueryUserPage params) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(params.getCardNo()), SysUser::getCardNo, params.getCardNo());
        wrapper.eq(ObjectUtil.isNotEmpty(params.getMobile()),SysUser::getMobile,params.getMobile());
        wrapper.like(ObjectUtil.isNotEmpty(params.getUserName()),SysUser::getUserName,params.getUserName());
        Page<SysUser> page = params.getPageEntity();
        return sysUserMapper.selectPage(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUser saveUser(SysUser user) {
        if (ObjectUtil.isEmpty(user.getId())){
            user.insert();
        }else {
            SysUser dbUser = getById(user.getId());
            BeanUtil.copyProperties(user,dbUser);
            dbUser.updateById();
        }
        publisher.publishEvent(new BaseAsyncEvent<>("我来了", EventConstant.TEST));
        return user;
    }

    @Override
    public List<SysUser> queryList(SysUser params) {
        QueryWrapper<SysUser> build = QueryFieldWrapperUtil.build(params);
        return list(build);

    }

    @Override
    public List<SysUser> queryUserList(UserQuery params) {
        QueryWrapper<SysUser> build = QueryFieldWrapperUtil.build(params, SysUser.class);
        return list(build);
    }
}
